﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FileOps;
using System.Threading;
using System.Windows;

namespace BAL.EstRec.SYNC
{
    public class SyncThread
    {
        public static Thread t = null;
        public static FolderSync fs;

        public SyncThread()
        {
        }


        public static bool CheckIsAlive()
        {

            if (SyncThread.t != null)
            {
                return SyncThread.t.IsAlive;

            }
            return false;

        }/////////


        public static void start(FolderSync fs, ISyncEventImp ev, bool onlyCat)
        {
            if (!SyncThread.CheckIsAlive())
            {
                fs.FileDownloadedEvent += delegate(object sender1, FolderSyncEventArgs e1)
                {
                    Action a = () =>
                    {
                        ev.fs_fileDownloaded(sender1, e1);
                    };
                    Application.Current.Dispatcher.BeginInvoke(a);
                };
                fs.FileErrorEvent += delegate(object sender2, FolderSyncEventArgs e2)
                {
                    Action a = () =>
                    {
                        ev.fs_FileErrorEvent(sender2, e2);
                    };
                    Application.Current.Dispatcher.BeginInvoke(a);
                };
                fs.FileUnzippedEvent += delegate(object sender3, FolderSyncEventArgs e3)
                {
                    Action a = () =>
                    {
                        ev.fs_FileUnzippedEvent(sender3, e3);
                    };
                    Application.Current.Dispatcher.BeginInvoke(a);
                };
                fs.FileUploadedEvent += delegate(object sender4, FolderSyncEventArgs e4)
                {
                    Action a = () =>
                    {
                        ev.fs_FileUploadedEvent(sender4, e4);
                    };
                    Application.Current.Dispatcher.BeginInvoke(a);
                };
                fs.FileEvent += delegate(object sender5, FolderSyncEventArgs e5)
                {
                    Action a = () =>
                    {
                        ev.fs_FileEvent(sender5, e5);
                    };
                    Application.Current.Dispatcher.BeginInvoke(a);
                };
                fs.SyncCompletedEvent += delegate(object sender6, FolderSyncEventArgs e6)
                {
                    Action a = () =>
                    {
                        ev.fs_SyncCompletedEvent(sender6, e6);
                    };
                    Application.Current.Dispatcher.BeginInvoke(a);
                };

                fs.CatStartSyncEvent += delegate(object sender7, FolderSyncEventArgs e7)
                {
                    Action a = () =>
                    {
                        ev.fs_CatStartSyncEvent(sender7, e7);
                    };
                    Application.Current.Dispatcher.BeginInvoke(a);
                };

                fs.CatEndSyncEvent += delegate(object sender8, FolderSyncEventArgs e8)
                {
                    Action a = () =>
                    {
                        ev.fs_CatEndSyncEvent(sender8, e8);
                    };
                    Application.Current.Dispatcher.BeginInvoke(a);
                };
                SyncThread.fs = fs;

                if (onlyCat)
                    SyncThread.t = new Thread(() => SyncThread.syncCatAction());
                else
                    SyncThread.t = new Thread(() => SyncThread.syncAction());

                SyncThread.t.IsBackground = true;
                SyncThread.t.Start();
            }
        }

        public static void syncAction()
        {
            SyncThread.fs.SyncCat(false);
            SyncThread.fs.loadData();
        }

        public static void syncCatAction()
        {
            SyncThread.fs.SyncCat(true);
        }

    }
}
